
#include "main.h"
#include "display.h"
#include "keybutton.h"
#include "eeprom.h"

#include <string.h>	/* for strlen */
#include <stdlib.h>	/* for atoi */


#define	LINE_MIN	5
#define	LINE_MAX	15

static char gszEdit[LCD_MAX_CHAR+1] = {0,};
static u32 gEdit = 0;

static void dispValue(u8 vdd, u8 line, u8 sel)
{
	switch (line) {
	case 5:		// Toff
		gEdit = gVout[vdd].dwToff;
		sprintf(gszLine, " Toff : %5d [mS]", gEdit);
		break;
	case 6:		// Tr1
		gEdit = gVout[vdd].dwTr1;
		sprintf(gszLine, "  Tr1 : %5d [uS]", gEdit);
		break;
	case 7:		// Vr1
		gEdit = gVout[vdd].dwVr1;
		sprintf(gszLine, "  Vr1 : %5d [mV]", gEdit);
		break;
	case 8:		// Tr2
		gEdit = gVout[vdd].dwTr2;
		sprintf(gszLine, "  Tr2 : %5d [uS]", gEdit);
		break;
	case 9:		// Vr2
		gEdit = gVout[vdd].dwVr2;
		sprintf(gszLine, "  Vr2 : %5d [mV]", gEdit);
		break;
	case 10:	// Tr3
		gEdit = gVout[vdd].dwTr3;
		sprintf(gszLine, "  Tr3 : %5d [uS]", gEdit);
		break;
	case 11:	// Vr3
		gEdit = gVout[vdd].dwVr3;
		sprintf(gszLine, "  Vr3 : %5d [mV]", gEdit);
		break;
	case 12:	// Tr4
		gEdit = gVout[vdd].dwTr4;
		sprintf(gszLine, "  Tr4 : %5d [uS]", gEdit);
		break;
	case 13:	// Vr4
		gEdit = gVout[vdd].dwVr4;
		sprintf(gszLine, "  Vr4 : %5d [mV]", gEdit);
		break;
	case 14:	// Ton
		gEdit = gVout[vdd].dwTon;
		sprintf(gszLine, "  Ton : %5d [mS]", gEdit);
		break;
	case 15:	// Tf1
		gEdit = gVout[vdd].dwTf1;
		sprintf(gszLine, "  Tf1 : %5d [uS]", gEdit);
		break;
	default:
		return;
	}

	LcdLineString(line, LCD_ALIGN_LEFT, gszLine, COLOR_BLACK, sel ? COLOR_WHITE : COLOR_YELLOW);
	if (sel) {
		sprintf(gszEdit, "%d", gEdit);
		LcdLineString(17, LCD_ALIGN_CENTER, gszEdit, COLOR_RED, COLOR_BLACK);
	}
}
static void updateVout(u8 vdd, u8 selLine)
{
	sprintf(gszLine, "<- VDD%d ->", vdd + 1);
	LcdLineString( 4, LCD_ALIGN_CENTER, gszLine, COLOR_YELLOW, COLOR_BLACK);

	for (u32 y=LINE_MIN; y<=LINE_MAX; y++) {
		dispValue(vdd, y, (y == selLine) ? 1 : 0);
	}
}

void modeSetup(u8 key, u32 first)
{
	static u8 _vdd = VOUT_1;	// 0(VDD1), 1(VDD2), 2(VDD3)
	static u8 _line = LINE_MIN;
	static u8 _first = 1;
	static const char *_num[] = {
		"7", "4", "1", "0", "8", "5", "2", "BS", "9", "6", "3"
	};
	u32 tmp;

	if (first) {
		LcdDrawRect(0, 0, LCD_SCREEN_WIDTH, LCD_SCREEN_HEIGHT, COLOR_BLACK);
		LcdLineString( 0, LCD_ALIGN_CENTER, STR_TITLE, COLOR_BLACK, COLOR_WHITE);
		LcdLineString(19, LCD_ALIGN_CENTER, "Mode : Setup", COLOR_BLACK, COLOR_WHITE);

		LcdLineString( 2, LCD_ALIGN_LEFT, "  [F3] Exit", COLOR_YELLOW, COLOR_BLACK);

		LcdLineString( 4, LCD_ALIGN_CENTER, "<- VDD1 ->", COLOR_YELLOW, COLOR_BLACK);
		LcdLineString(18, LCD_ALIGN_CENTER, "[.]Cancel Save[OK]", COLOR_YELLOW, COLOR_BLACK);

		_vdd = VOUT_1;
		_line = LINE_MIN;
		_first = 1;

		for (u32 y=LINE_MIN; y<=LINE_MAX; y++) {
			dispValue(_vdd, y, (LINE_MIN == y) ? 1 : 0);
		}
	}

	switch (key) {
	case KEYBUTTON_0:
	case KEYBUTTON_1:
	case KEYBUTTON_2:
	case KEYBUTTON_3:
	case KEYBUTTON_4:
	case KEYBUTTON_5:
	case KEYBUTTON_6:
	case KEYBUTTON_7:
	case KEYBUTTON_8:
	case KEYBUTTON_9:
		if (_first) {
			_first = 0;
			gszEdit[0] = 0;
		}
		if (5 > strlen(gszEdit)) {
			strcat(gszEdit, _num[key-KEYBUTTON_7]);
			LcdLineString(17, LCD_ALIGN_CENTER, gszEdit, COLOR_YELLOW, COLOR_BLACK);
		}
		break;
	case KEYBUTTON_DOT:
		_first = 1;
		sprintf(gszEdit, "%d", gEdit);
		LcdLineString(17, LCD_ALIGN_CENTER, gszEdit, COLOR_YELLOW, COLOR_BLACK);
		break;
	case KEYBUTTON_BS:
		if (_first) {
			_first = 0;
			gszEdit[0] = 0;
		}
		if (tmp = strlen(gszEdit)) {
			gszEdit[tmp - 1] = 0;
			LcdLineString(17, LCD_ALIGN_CENTER, gszEdit, COLOR_YELLOW, COLOR_BLACK);
		}
		break;
	case KEYBUTTON_UP:
		tmp = _line--;
		if (LINE_MIN > _line) {
			_line = LINE_MAX;
		}
		_first = 1;
		dispValue(_vdd, tmp, 0);
		dispValue(_vdd, _line, 1);
		break;
	case KEYBUTTON_DOWN:
		tmp = _line++;
		if (LINE_MAX < _line) {
			_line = LINE_MIN;
		}
		_first = 1;
		dispValue(_vdd, tmp, 0);
		dispValue(_vdd, _line, 1);
		break;
	case KEYBUTTON_LEFT:
		if (VOUT_1 == _vdd) {
			_vdd = VOUT_3;
		} else {
			_vdd--;
		}
		_first = 1;
		updateVout(_vdd, _line);
		break;
	case KEYBUTTON_RIGHT:
		if (VOUT_3 == _vdd) {
			_vdd = VOUT_1;
		} else {
			_vdd++;
		}
		_first = 1;
		updateVout(_vdd, _line);
		break;
	case KEYBUTTON_F3:
		gvCurMode = MODE_READY;
		break;
	case KEYBUTTON_OK:
		{
			DWORD *pVout = (DWORD *)&gVout[_vdd];
			u32 addr = 0;

			tmp = _line - LINE_MIN;
			pVout[tmp] = atoi(gszEdit);
			if (tmp && (0 == tmp%2)) {
				// 2(Vr1), 4(Vr2), 6(Vr3), 8(Vr4)
				if (2000 < pVout[tmp]) {
					pVout[tmp] = 2000;
					sprintf(gszEdit, "%d", 2000);
				}
			} else {
				// 0(Toff), 1(Tr1), 3(Tr2), 5(Tr3), 7(Tr4), 9(Ton), 10(Tf1)
				if (99999 < pVout[tmp]) {
					pVout[tmp] = 99999;
					sprintf(gszEdit, "%d", 99999);
				}
			}

			switch (_vdd) {
			case VOUT_1:
				addr = EEPROM_VOUT1_TOFF_L + tmp*2;
				break;
			case VOUT_2:
				addr = EEPROM_VOUT2_TOFF_L + tmp*2;
				break;
			case VOUT_3:
				addr = EEPROM_VOUT3_TOFF_L + tmp*2;
				break;
			}
			EE_WriteVariable32(addr, pVout[tmp]);

			dispValue(_vdd, _line, 1);
			_first = 1;
		}
		break;
	}
}

